
//部门
var xhr = new XMLHttpRequest();
//创建请求
xhr.open("get","http://192.168.192.100:8080/BaorOASys/dept/deptList");
//发送请求
xhr.send();
//监听请求
xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
        var res = JSON.parse(xhr.responseText);
        //console.log(res);
        var data = res.dataset;
        var dept = document.getElementById("Dept");
        var dept1 = document.getElementById("Dept1");
        //添加用户模态框中的部门
        for(var i=0;i<data.length;i++){
            var newoption = document.createElement("option");
            newoption.innerHTML= data[i].deptName;
            newoption.setAttribute("value",data[i].deptId);
            dept.appendChild(newoption);
        }
        //修改用户模态框中的部门
        for(var i=0;i<data.length;i++){
            var newoption = document.createElement("option");
            newoption.innerHTML= data[i].deptName;
            newoption.setAttribute("value",data[i].deptId);
            dept1.appendChild(newoption);
        }
    }
}



//添加用户接口
//点击加号添加用户，弹出模态框
//获取模态框
var addUser = document.getElementsByClassName("addUser")[0];
var addi = document.getElementsByClassName("addi")[0];
addi.onclick = function(){
    addUser.style.display = "block";
}
//添加用户模态框中的账号
var date = new Date();
var month = date.getMonth();
if(month<9){
    month = "0"+(month+1);  //月
}
var day = date.getDate();
var num = Math.floor(Math.random()*100);
if(num<10){
    num = "0"+num;  //后两位数字
}
//获取账号框
var number = document.getElementsByClassName("number")[0];
number.innerHTML = "br"+month+day+num;
//性别
var Sex = document.getElementById("Sex");
//部门
var Dept =document.getElementById("Dept");
//角色
var Role = document.getElementById("Role");
//点击添加用户模态框中的确定或取消，模态框消失
var addUser = document.getElementsByClassName("addUser")[0];
var sure = document.getElementsByClassName("sure")[0];
sure.onclick = function(){
    addUser.style.display = "none";
    //模态框中所有input框
    var inputs = addUser.getElementsByTagName("input");
    var arrUser = [];
    for(var i=0;i<inputs.length;i++){
        arrUser[i] = inputs[i].value;
    }
    arrUser.push(Sex.value);
    arrUser.push(Dept.value);

    console.log(arrUser);
    var Json2 = {
        userDept:Dept.value,
        userAccount:number.innerText,
        userRole:Role.value,
        userPhone:arrUser[1],
        userPassword:arrUser[2],
        userName:arrUser[0],
        userGender:Sex.value
    }
    console.log(Json2);
// 检测账号是否重复
    ajax.post(ajax.myUrl+"users/checkUser",Json2.userAccount,function(result){
        console.log(result);
        // 添加用户
        ajax.post(ajax.myUrl+"users/addUser",Json2,function(result){
            console.log(result)
        })
    })

}
var cancel = document.getElementsByClassName("cancel")[0];
cancel.onclick = function(){
    addUser.style.display = "none";
}


// //初始页码
// var pageIndex = 1;
// //每页条数
// var pageSize = 5;
// 页数
var pageCount = 0;
function classOnload(a){
    console.log(a);
// }

//用户列表
var Json = {
    "pageIndex":a,
    "pageSize":9
}
ajax.post(ajax.myUrl+"users/userList",Json,function(result){
    console.log(result);
    response(result);
    var data = result.dataset.userList;
    var tbody = document.querySelector("tbody");
    tbody.innerHTML = "";
    var addUser1 = document.getElementsByClassName("addUser1")[0];
    for(var i=0;i<data.length;i++){
        var newtr = document.createElement("tr");
        newtr.innerHTML=`
        <td><input type="checkbox"></td>
        <td>${data[i].userName}</td>
        <td>${data[i].userDept}</td>
        <td>${data[i].userGender}</td>
        <td>${data[i].userPhone}</td>
        <td>${data[i].userRole}</td>
        <td><div class="remove">修改</div><div class="delete">删除</div></td>
        `;
        tbody.appendChild(newtr);
        localStorage.setItem("userId"+i,data[i].userId);
    }


    //点击修改，弹出模态框 
    var remove = document.getElementsByClassName("remove");
    for(var m=0;m<data.length;m++){
        remove[m].onclick = function(){
            for(var j=0;j<data.length;j++){
                if(this == remove[j]){
                    var addUser1 = document.getElementsByClassName("addUser1")[0];
                    addUser1.style.display = "block";
                    var userid = Number(data[j].userId);
                    var xhr = new XMLHttpRequest();
                    xhr.open("get","http://192.168.192.100:8080/BaorOASys/users/userInfo?userId="+userid);
                    xhr.send();
                    xhr.onreadystatechange = function (){
                        if(xhr.readyState == 4 && xhr.status == 200){
                            var res = JSON.parse(xhr.responseText);
                            console.log(res.dataset);
                            //获取修改用户模态框
                            var addUser1 = document.getElementsByClassName("addUser1")[0];
                            addUser1.querySelector(".username").value = res.dataset.userName;
                            // addUser1.querySelector(".userRole1").value = res.dataset.userRole;
                            addUser1.querySelector(".number1").innerHTML = res.dataset.userAccount;
                            addUser1.querySelector(".phoneNum").value = res.dataset.userPhone;
                            addUser1.querySelector(".password1").value = res.dataset.userPassword;
                        }
                    }

                    //点击修改用户模态框中的确定或取消，模态框消失,并传回数据
                    var sure1 = document.getElementsByClassName("sure1")[0];
                    sure1.onclick = function(){
                        //获取修改用户模态框
                        var addUser1 = document.getElementsByClassName("addUser1")[0];
                        var username = addUser1.getElementsByClassName("username")[0].value;
                        var userphone = addUser1.getElementsByClassName("phoneNum")[0].value;
                        console.log(userphone);
                        var Sex1 = document.getElementById("Sex1");
                        console.log(Sex1.value);
                        var Dept1 =document.getElementById("Dept1");
                        var Role1 = document.getElementById("Role1");
                        console.log(Dept1.value);
                        // console.log(Sex1.value,Dept1.value,Role1.value);
                        var Json4 = {
                            userId:userid,
                            userName:username,
                            userGender:Sex1.value,
                            userDept:Dept1.value,
                            userPhone:userphone,
                            userRole:Role1.value
                        }
                        console.log(Json4);
                        ajax.post(ajax.myUrl+"users/updateUser",Json4,function(result){
                            console.log(result);
                        })
                        addUser1.style.display = "none";
                    }
                    var cancel1 = document.getElementsByClassName("cancel1")[0];
                    cancel1.onclick = function(){
                        addUser1.style.display = "none";
                    }
                }
            }
        }
    }



    //点击删除，弹出提醒
    var dele = document.getElementsByClassName("delete");
    console.log(dele[0]);
    for(var i=0;i<dele.length;i++){
        dele[i].onclick = function(){
            for(var j=0;j<data.length;j++){
                if(this == dele[j]){
                    var userid = Number(data[j].userId);
                    //点击删除，弹出提示
                    var tip = confirm("确认要删除吗？");
                    if(tip == true){
                        // console.log("111");
                        //点击确定，触发事件
                        var Json5 = {
                            userId:userid,
                            // delflag:1
                        }
                        console.log(Json5);
                        ajax.post(ajax.myUrl+"users/deleteUser",Json5,function(result){
                            console.log(result);
                        })
                    }
                }
            }
        }
    }

})

}

//点击搜索实现按姓名搜索
var search = document.getElementsByClassName("search")[0];
var btn = search.getElementsByTagName("button")[0];
var input = search.getElementsByTagName("input")[0];
btn.onclick = function(){
    console.log(input.value);
    var Json3={
        userName:input.value,
        pageIndex:1,
        pageSize:5
    }
    ajax.post("http://192.168.192.100:8080/BaorOASys/users/userList",Json3,function(result){
        console.log(result);
        var data = result.dataset.userList;
        console.log(data);
        if(data.length == 0){
            alert("该用户不存在,请重新输入");
        }else{
            var tbody = document.querySelector("tbody");
            tbody.innerHTML = "";
            for(var i=0;i<data.length;i++){
                var newtr = document.createElement("tr");
                newtr.innerHTML=`
                <td><input type="checkbox"></td>
                <td>${data[i].userName}</td>
                <td>${data[i].userDept}</td>
                <td>${data[i].userGender}</td>
                <td>${data[i].userPhone}</td>
                <td>${data[i].userRole}</td>
                <td><div class="remove">修改</div><div class="delete">删除</div></td>
                `;
                tbody.appendChild(newtr);
                // localStorage.setItem("userId"+i,data[i].userId);
            }
        }
        
        //点击修改，弹出模态框 
    var remove = document.getElementsByClassName("remove");
    for(var m=0;m<data.length;m++){
        remove[m].onclick = function(){
            for(var j=0;j<data.length;j++){
                if(this == remove[j]){
                    var addUser1 = document.getElementsByClassName("addUser1")[0];
                    addUser1.style.display = "block";
                    var userid = Number(data[j].userId);
                    var xhr = new XMLHttpRequest();
                    xhr.open("get","http://192.168.192.100:8080/BaorOASys/users/userInfo?userId="+userid);
                    xhr.send();
                    xhr.onreadystatechange = function (){
                        if(xhr.readyState == 4 && xhr.status == 200){
                            var res = JSON.parse(xhr.responseText);
                            console.log(res.dataset);
                            //获取修改用户模态框
                            var addUser1 = document.getElementsByClassName("addUser1")[0];
                            addUser1.querySelector(".username").value = res.dataset.userName;
                            // addUser1.querySelector(".userRole1").value = res.dataset.userRole;
                            addUser1.querySelector(".number1").innerHTML = res.dataset.userAccount;
                            addUser1.querySelector(".phoneNum").value = res.dataset.userPhone;
                            addUser1.querySelector(".password1").value = res.dataset.userPassword;
                        }
                    }




                    //点击修改用户模态框中的确定或取消，模态框消失,并传回数据
                    var sure1 = document.getElementsByClassName("sure1")[0];
                    sure1.onclick = function(){
                        //获取修改用户模态框
                        var addUser1 = document.getElementsByClassName("addUser1")[0];
                        var username = addUser1.getElementsByClassName("username")[0].value;
                        var userphone = addUser1.getElementsByClassName("phoneNum")[0].value;
                        console.log(userphone);
                        var Sex1 = document.getElementById("Sex1");
                        console.log(Sex1.value);
                        var Dept1 =document.getElementById("Dept1");
                        var Role1 = document.getElementById("Role1");
                        console.log(Dept1.value);
                        // console.log(Sex1.value,Dept1.value,Role1.value);
                        var Json4 = {
                            userId:userid,
                            userName:username,
                            userGender:Sex1.value,
                            userDept:Dept1.value,
                            userPhone:userphone,
                            userRole:Role1.value
                        }
                        console.log(Json4);
                        ajax.post("http://192.168.192.100:8080/BaorOASys/users/updateUser",Json4,function(result){
                            console.log(result);
                        })
                        addUser1.style.display = "none";
                    }
                    var cancel1 = document.getElementsByClassName("cancel1")[0];
                    cancel1.onclick = function(){
                        addUser1.style.display = "none";
                    }
                }
            }
        }
    }


    //点击删除，弹出提醒
    var dele = document.getElementsByClassName("delete");
    console.log(dele[0]);
    for(var i=0;i<dele.length;i++){
        dele[i].onclick = function(){
            for(var j=0;j<data.length;j++){
                if(this == dele[j]){
                    var userid = Number(data[j].userId);
                    //点击删除，弹出提示
                    var tip = confirm("确认要删除吗？");
                    if(tip == true){
                        // console.log("111");
                        //点击确定，触发事件
                        var Json5 = {
                            userId:userid,
                            // delflag:1
                        }
                        console.log(Json5);
                        ajax.post("http://192.168.192.100:8080/BaorOASys/users/deleteUser",Json5,function(result){
                            console.log(result);
                        })
                    }
                }
            }
        }
    }

    })
}



// 鼠标经过加号显示要添加的内容
var addition = document.getElementsByClassName("addition")[0];
var addi = addition.getElementsByClassName("addi")[0];
var add = addition.getElementsByClassName("add")[0];
addi.onmouseover = function(){
    add.style.display = "block";
}
addi.onmouseout = function(){
    add.style.display = "none";
}
//鼠标经过垃圾桶删除用户
var dele = addition.getElementsByClassName("dele")[0];
var add1 = addition.getElementsByClassName("add1")[0];
dele.onmouseover = function(){
    add1.style.display = "block";
}
dele.onmouseout = function(){
    add1.style.display = "none";
}












//修改用户模态框中的账号
var date = new Date();
var month = date.getMonth();
if(month<9){
    month = "0"+(month+1);  //月
}
var day = date.getDate();
var num = Math.floor(Math.random()*100);
if(num<10){
    num = "0"+num;  //月
}
//获取账号框
var number1 = document.getElementsByClassName("number1")[0];
//console.log("br"+month+day+num);
number1.innerHTML = "br"+month+day+num;

